var pjgl = {
    setBtnStatus:function () {
        var row = $('#grid_pjgl').datagrid('getSelected');
        var type =$("[name='pjsflx_type']").val();
        if(type==0){//应收票据
            //应收票据的状态可以有 1 可操作 2.已贴现 3.已托收   6已兑换
            if (row && row.status==1) { //被选择的可操作应收票据
                $("#btn_pjgl_add").hide();
                $("#btn_pjgl_mod").show();
                $("#btn_pjgl_del").show();
                $("#btn_pjgl_pjtx").show();
                $("#btn_pjgl_pjts").show();
                $("#btn_pjgl_pjcd").hide();
                $("#btn_pjgl_pjdh").show();
                $("#btn_pjgl_pjtxqx").hide();
                $("#btn_pjgl_pjtsqx").hide();
                $("#btn_pjgl_pjcdqx").hide();
                $("#btn_pjgl_pjdhqx").hide();
            }
            if (row && row.status==2) { //被选择的已贴现应收票据
                $("#btn_pjgl_add").hide();
                $("#btn_pjgl_mod").hide();
                $("#btn_pjgl_del").hide();
                $("#btn_pjgl_pjtx").hide();
                $("#btn_pjgl_pjts").hide();
                $("#btn_pjgl_pjcd").hide();
                $("#btn_pjgl_pjdh").hide();
                $("#btn_pjgl_pjtxqx").show();
                $("#btn_pjgl_pjtsqx").hide();
                $("#btn_pjgl_pjcdqx").hide();
                $("#btn_pjgl_pjdhqx").hide();
            }
            if (row && row.status==3) { //被选择的已托收应收票据
                $("#btn_pjgl_add").hide();
                $("#btn_pjgl_mod").hide();
                $("#btn_pjgl_del").hide();
                $("#btn_pjgl_pjtx").hide();
                $("#btn_pjgl_pjts").hide();
                $("#btn_pjgl_pjcd").hide();
                $("#btn_pjgl_pjdh").hide();
                $("#btn_pjgl_pjtxqx").hide();
                $("#btn_pjgl_pjtsqx").show();
                $("#btn_pjgl_pjcdqx").hide();
                $("#btn_pjgl_pjdhqx").hide();
            }
            if (row && row.status==6) { //被选择的已兑换应收票据
                $("#btn_pjgl_add").hide();
                $("#btn_pjgl_mod").hide();
                $("#btn_pjgl_del").hide();
                $("#btn_pjgl_pjtx").hide();
                $("#btn_pjgl_pjts").hide();
                $("#btn_pjgl_pjcd").hide();
                $("#btn_pjgl_pjdh").hide();
                $("#btn_pjgl_pjtxqx").hide();
                $("#btn_pjgl_pjtsqx").hide();
                $("#btn_pjgl_pjcdqx").hide();
                $("#btn_pjgl_pjdhqx").show();
            }else if(!row){
                $("#btn_pjgl_add").hide();
                $("#btn_pjgl_mod").hide();
                $("#btn_pjgl_del").hide();
                $("#btn_pjgl_pjtx").hide();
                $("#btn_pjgl_pjts").hide();
                $("#btn_pjgl_pjcd").hide();
                $("#btn_pjgl_pjdh").hide();
                $("#btn_pjgl_pjtxqx").hide();
                $("#btn_pjgl_pjtsqx").hide();
                $("#btn_pjgl_pjcdqx").hide();
                $("#btn_pjgl_pjdhqx").hide();
            }
        }else{
            if (row && row.status==1) { //被选择的可操作的应付票据
                $("#btn_pjgl_add").show();
                $("#btn_pjgl_mod").show();
                $("#btn_pjgl_del").show();
                $("#btn_pjgl_pjtx").hide();
                $("#btn_pjgl_pjts").hide();
                $("#btn_pjgl_pjcd").hide();
                $("#btn_pjgl_pjdh").hide();
                $("#btn_pjgl_pjtxqx").hide();
                $("#btn_pjgl_pjtsqx").hide();
                $("#btn_pjgl_pjcdqx").hide();
                $("#btn_pjgl_pjdhqx").hide();
            }if (row && row.status==4) { //被选择的承兑的应付票据
                $("#btn_pjgl_add").hide();
                $("#btn_pjgl_mod").hide();
                $("#btn_pjgl_del").hide();
                $("#btn_pjgl_pjtx").hide();
                $("#btn_pjgl_pjts").hide();
                $("#btn_pjgl_pjcd").hide();
                $("#btn_pjgl_pjdh").hide();
                $("#btn_pjgl_pjtxqx").hide();
                $("#btn_pjgl_pjtsqx").hide();
                $("#btn_pjgl_pjcdqx").show();
                $("#btn_pjgl_pjdhqx").hide();
            }if (row && row.status==5) { //被选择的已使用的应付票据
                $("#btn_pjgl_add").hide();
                $("#btn_pjgl_mod").hide();
                $("#btn_pjgl_del").hide();
                $("#btn_pjgl_pjtx").hide();
                $("#btn_pjgl_pjts").hide();
                $("#btn_pjgl_pjcd").show();
                $("#btn_pjgl_pjdh").hide();
                $("#btn_pjgl_pjtxqx").hide();
                $("#btn_pjgl_pjtsqx").hide();
                $("#btn_pjgl_pjcdqx").hide();
                $("#btn_pjgl_pjdhqx").hide();
            }else if(!row){
                $("#btn_pjgl_add").show();
                $("#btn_pjgl_mod").hide();
                $("#btn_pjgl_del").hide();
                $("#btn_pjgl_pjtx").hide();
                $("#btn_pjgl_pjts").hide();
                $("#btn_pjgl_pjcd").hide();
                $("#btn_pjgl_pjdh").hide();
                $("#btn_pjgl_pjtxqx").hide();
                $("#btn_pjgl_pjtsqx").hide();
                $("#btn_pjgl_pjcdqx").hide();
                $("#btn_pjgl_pjdhqx").hide();
            }
        }
    }
}



$(function () {
    $("#pjgl_gsId").combotree({
        loadFilter:function (rows) {
            return convert(rows);
        }
    });
    var pjsflxType = $("[name='pjsflx_type']").val();
    var clickRowIndex = null;
    var sysDate = $("#pjgl_sysDate").val();
    //表格初始加载
        if( pjsflxType==0 || !pjsflxType){
            $('#grid_pjgl').datagrid({
                url:$.webPath() + "/zjgl/pjgl/pjglList?pjsflxType="+pjsflxType,
                toolbar:"#toolbar_pjgl",
                fitColumns:true,
                singleSelect:false,
                checkOnSelect:true,
                pagination:true,
                pageNumber:1,
                fit:true,
                rownumbers:true,
                striped:true,
                showFooter :true,
                columns:[[
                    {checkbox:true},
                    {field:'nbgs_jc', title:'公司名称', width:20,align:'center'},
                    {field:'pjlx', title:'票据类型', width:13,align:'center',
                        formatter:function (value) {
                            if (value == '0')
                                return '银票';
                            else if ( value ==  '1')
                                return '商票';
                        }},
                    {field:'pjhm', title:'票据号码', width:17,align:'center'},
                    {field:'sk_bm', title:'收款号码', width:18,align:'center'},
                    {field:'yscpyh', title:'出票银行', width:23,align:'center'},
                    {field:'cprq', title:'出票日', width:20,align:'center',
                        formatter:function(value){
                            var val = formatDate(sysDate,value,5);
                            return val
                        }},
                    {field:'dqr', title:'到期日', width:20,align:'center',
                        formatter:function(value){
                            var val = formatDate(sysDate,value,5);
                            return val
                     }},
                    {field:'dqje', title:'到期金额', width:15,align:'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }
                    },
                    {field:'cpr', title:'出票人', width:12,align:'center'},
                    {field:'skr', title:'收款人', width:12,align:'center'},
                    {field:'jpdw_qc', title:'交款单位', width:12,align:'center'},
                    {field:'yslx', title:'应收利息', width:10,align:'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }
                    },
                    {field:"syrq" ,title:"使用日期",width:20,align:'center'},
                    {field:"bz" ,title:"备注",width:15,align:'center'},
                    {field:"status" ,title:"状态",width:15,align:'center',
                        formatter:function (value) {
                        if (value == 1)
                                return '可操作';
                        if (value == 2)
                            return '已贴现';
                        if(value== 3)
                            return '已托收';
                        if(value== 4)
                            return '已承兑';
                        if(value== 5 )
                            return '已背书';
                        if(value== 6 )
                            return '已兑换';
                    }}]],
                onLoadSuccess:function(){
                    pjgl.setBtnStatus();
                    var rows = $('#grid_pjgl').datagrid("getRows");
                    if(rows){
                        $('#grid_pjgl').datagrid("selectRow", 0);
                        clickRowIndex = 0;
                    }
                    $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
                },
                onSelect:function(rowIndex, rowData){
                    pjgl.setBtnStatus();
                    if (clickRowIndex == rowIndex)
                        return;
                    clickRowIndex = rowIndex;
                    var id = rowData.id;
                    var detailPanel = $("#layout_pjgl").layout("panel", "center");
                    $(detailPanel).panel("open");
                    $(detailPanel).panel({href: $.webPath() + "/zjgl/pjgl/pjgl_pjdh_detail?pjId=" + id });
                },
                onCheck :function(){
                    pjgl.setBtnStatus();
                }
            });
        } else{
            $('#grid_pjgl').datagrid({
                url:$.webPath() + "/zjgl/pjgl/pjglList?pjsflxType="+pjsflxType,
                toolbar:"#toolbar_pjgl",
                fitColumns:true,
                singleSelect:false,
                checkOnSelect:true,
                pagination:true,
                pageNumber:1,
                fit:true,
                rownumbers:true,
                striped:true,
                showFooter :true,
                columns:[[
                    {checkbox:true},
                    {field:'nbgs_jc', title:'公司名称', width:25,align:'center'},
                    {field:'pjlx', title:'票据类型', width:15, align:'center',
                        formatter:function (value) {
                            if (value == '0')
                                return '银票';
                            else if ( value ==  '1')
                                return '商票';
                        }},
                    {field:'pjhm', title:'票据号码', width:20,align:'center'},
                    {field:'sfk_bm', title:'付款号码', width:20,align:'center'},
                    {field:'sfk_sfkrq', title:'付款日期', width:20,align:'center'},
                    {field:'yfcpyh_qc', title:'出票行', width:35,align:'center'},
                    {field:'dqje', title:'金额', width:20,align:'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }
                    },
                    {field:'bzj', title:'保证金', width:15,align:'right', formatter:function(value){
                        var sl = formatJe2(value,",");
                        return sl;
                    }},
                    {field:'cprq', title:'出票日', width:20,align:'center',
                        formatter:function(value){
                            var val = formatDate(sysDate,value,5);
                            return val
                        }},
                    {field:'dqr', title:'到期日', width:20,align:'center',
                        formatter:function(value){
                            var val = formatDate(sysDate,value,5);
                            return val
                    }},
                    {field:'spr_jc', title:'收票人', width:25,align:'center'},
                    {field:'yflx', title:'应付利息', width:15,align:'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }
                    },
                    {field:"syrq" ,title:"使用日期",width:20,align:'center'},
                    {field:"bz" ,title:"备注",width:20,align:'center'},
                    {field:"status" ,title:"状态",width:15,align:'center',
                        formatter:function (value) {
                            if (value == 1)
                                return '可操作';
                            if (value == 2)
                                return '已贴现';
                            if(value== 3)
                                return '已托收';
                            if(value== 4)
                                return '已承兑';
                            if(value== 5 )
                                return '已使用';
                            if(value== 6 )
                                return '已兑换';
                        }}]],
                onLoadSuccess:function(){
                    pjgl.setBtnStatus();
                    $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
                },
                onClickRow:function(){
                    pjgl.setBtnStatus();
                },
                onCheck :function(){
                    pjgl.setBtnStatus();
                }
            });
        }

    //当类型切换时切换页面
    $("[name='pjsflx_type']").combobox({onSelect:function () {
        var pjsflxType = $("[name='pjsflx_type']").val();
        if(pjsflxType==0 || !pjsflxType){
            $("#pj_zt").combobox({
                data:[{
                    label: '全部',
                    value: ''
                },{
                    label: '可操作',
                    value: '1'
                },{
                    label: '已背书',
                    value: '5'
                },{
                    label: '已贴现',
                    value: '2'
                },{
                    label: '已托收',
                    value: '3'
                },{
                    label: '已兑换',
                    value: '6'
                }]
            });
        }else{
            $("#pj_zt").combobox({
                data:[{
                    label: '全部',
                    value: ''
                },{
                    label: '可操作',
                    value: '1'
                },{
                    label: '已使用',
                    value: '5'
                },{
                    label: '已承兑',
                    value: '4'
                }]
            });
        }
        if(pjsflxType==0 || !pjsflxType){
            $('#grid_pjgl').datagrid({
                url:$.webPath() + "/zjgl/pjgl/pjglList?pjsflxType="+pjsflxType,
                toolbar:"#toolbar_pjgl",
                fitColumns:true,
                singleSelect:false,
                checkOnSelect:true,
                pagination:true,
                pageNumber:1,
                fit:true,
                rownumbers:true,
                striped:true,
                showFooter :true,
                columns:[[
                    {checkbox:true},
                    {field:'nbgs_jc', title:'公司名称', width:20,align:'center'},
                    {field:'pjlx', title:'票据类型', width:13,align:'center',
                        formatter:function (value) {
                            if (value == '0')
                                return '银票';
                            else if ( value ==  '1')
                                return '商票';
                        }},
                    {field:'pjhm', title:'票据号码', width:17,align:'center'},
                    {field:'sk_bm', title:'收款号码', width:18,align:'center'},
                    {field:'yscpyh', title:'出票银行', width:23,align:'center'},
                    {field:'cprq', title:'出票日', width:20,align:'center',
                        formatter:function(value){
                            var val = formatDate(sysDate,value,5);
                            return val
                        }},
                    {field:'dqr', title:'到期日', width:20,align:'center',
                        formatter:function(value){
                            var val = formatDate(sysDate,value,5);
                            return val
                        }},
                    {field:'dqje', title:'到期金额', width:15,align:'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }
                    },
                    {field:'cpr', title:'出票人', width:12,align:'center'},
                    {field:'skr', title:'收款人', width:12,align:'center'},
                    {field:'jpdw_qc', title:'交款单位', width:12,align:'center'},
                    {field:'yslx', title:'应收利息', width:10,align:'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }
                    },
                    {field:"syrq" ,title:"使用日期",width:20,align:'center'},
                    {field:"bz" ,title:"备注",width:15,align:'center'},
                    {field:"status" ,title:"状态",width:15,align:'center',
                        formatter:function (value) {
                            if (value == 1)
                                return '可操作';
                            if (value == 2)
                                return '已贴现';
                            if(value== 3)
                                return '已托收';
                            if(value== 4)
                                return '已承兑';
                            if(value== 5 )
                                return '已背书';
                            if(value== 6 )
                                return '已兑换';
                        }}]],
                onLoadSuccess:function(){
                    pjgl.setBtnStatus();
                    var rows = $('#grid_pjgl').datagrid("getRows");
                    if(rows){
                        $('#grid_pjgl').datagrid("selectRow", 0);
                        clickRowIndex = 0;
                    }
                    $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
                },
                onSelect:function(rowIndex, rowData){
                    pjgl.setBtnStatus();
                    if (clickRowIndex == rowIndex)
                        return;
                    clickRowIndex = rowIndex;
                    var id = rowData.id;
                    var detailPanel = $("#layout_pjgl").layout("panel", "center");
                    $(detailPanel).panel("open");
                    $(detailPanel).panel({href: $.webPath() + "/zjgl/pjgl/pjgl_pjdh_detail?pjId=" + id });
                },
                onCheck :function(){
                    pjgl.setBtnStatus();
                }
            });
        } else{
            $('#grid_pjgl').datagrid({
                url:$.webPath() + "/zjgl/pjgl/pjglList?pjsflxType="+pjsflxType,
                toolbar:"#toolbar_pjgl",
                fitColumns:true,
                singleSelect:false,
                checkOnSelect:true,
                pagination:true,
                pageNumber:1,
                fit:true,
                rownumbers:true,
                striped:true,
                showFooter :true,
                columns:[[
                    {checkbox:true},
                    {field:'nbgs_jc', title:'公司名称', width:25,align:'center'},
                    {field:'pjlx', title:'票据类型', width:15,align:'center',
                        formatter:function (value) {
                            if (value == '0')
                                return '银票';
                            else if ( value ==  '1')
                                return '商票';
                        }},
                    {field:'pjhm', title:'票据号码', width:20,align:'center'},
                    {field:'sfk_bm', title:'付款号码', width:20,align:'center'},
                    {field:'sfk_sfkrq', title:'付款日期', width:20,align:'center'},
                    {field:'yfcpyh_qc', title:'出票行', width:35,align:'center'},
                    {field:'dqje', title:'金额', width:20, align:'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }
                    },
                    {field:'bzj', title:'保证金', width:15, formatter:function(value){
                        var sl = formatJe2(value,",");
                        return sl;
                    },align:'right'},
                    {field:'cprq', title:'出票日', width:20,align:'center',
                        formatter:function(value){
                            var val = formatDate(sysDate,value,5);
                            return val
                        }},
                    {field:'dqr', title:'到期日', width:20,align:'center',
                        formatter:function(value){
                            var val = formatDate(sysDate,value,5);
                            return val
                        }},
                    {field:'spr_jc', title:'收票人', width:25,align:'center'},
                    {field:'yflx', title:'应付利息', width:15,align:'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }
                    },
                    {field:"syrq" ,title:"使用日期",width:20,align:'center'},
                    {field:"bz" ,title:"备注",width:20,align:'center'},
                    {field:"status" ,title:"状态",width:15,align:'center',
                        formatter:function (value) {
                            if (value == 1)
                                return '可操作';
                            if (value == 2)
                                return '已贴现';
                            if(value== 3)
                                return '已托收';
                            if(value== 4)
                                return '已承兑';
                            if(value== 5 )
                                return '已使用';
                            if(value== 6 )
                                return '已兑换';
                        }}]],
                onLoadSuccess:function(){
                    pjgl.setBtnStatus();
                    $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
                },
                onClickRow:function(){
                    pjgl.setBtnStatus();
                },
                onCheck :function(){
                    pjgl.setBtnStatus();
                }
            });
        }
        $('#grid_pjgl').datagrid("reload");
    }});
    //**********按钮触发事件**********
    //新增按钮
    $("#btn_pjgl_add").click(function () {
        pjgl_add('新增应付票据', 'add_pjgl');
    });

    //查询按钮
    $("#btn_pjgl_search").click(function () {
        pjgl_search();
    });

    $("#btn_pjgl_mod").click(function () {
        pjgl_modefy('修改票据', 'update_pjgl');
    });

    $("#btn_pjgl_pjdh").click(function () {
        pjgl_dh('票据兑换', 'pjdh_pjgl');
    });


    $("#btn_pjgl_del").click(function () {
        pjgl_delete();
    });

    $("#btn_pjgl_pjtx").click(function () {
        pjgl_pjtx('票据贴现', 'pjtx_pjgl');
    });

    $("#btn_pjgl_pjts").click(function () {
        pjgl_pjts('票据托收', 'pjts_pjgl');
    });

    $("#btn_pjgl_pjcd").click(function () {
        pjgl_pjcd('票据承兑', 'pjcd_pjgl');
    });

    //票据贴现取消   暂时不用 在贴现承兑界面取消
//    $("#btn_pjgl_pjtxqx").click(function () {
//        pjgl_txqx();
//    });

    //票据托收取消  在贴现承兑界面取消
//    $("#btn_pjgl_pjtsqx").click(function () {
//        pjgl_tsqx();
//    });

    //票据承兑取消  在贴现承兑界面取消
//    $("#btn_pjgl_pjcdqx").click(function () {
//        pjgl_cdqx();
//    });

    //票据兑换取消
    $("#btn_pjgl_pjdhqx").click(function () {
        pjgl_dhqx();
    });

    $("#pj_zt").combobox({
        data:[{
            label: '全部',
            value: ''
        },{
            label: '可操作',
            value: '1'
        },{
            label: '已贴现',
            value: '2'
        },{
            label: '已背书',
            value: '5'
        },{
            label: '已托收',
            value: '3'
        },{
            label: '已兑换',
            value: '6'
        }],
        onLoadSuccess:function(){
            $("#pj_zt").combobox('setValue','');
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        }
    });


    //企业机构导出
    $("#pj_dc").click(function () {
        var pjsflxType =  $("[name='pjsflx_type']").val();
        var pjlxType = $("[name='pjlx_type']").val();
        var gsId = $("#pjgl_gsId").combotree('getValue') == null ? null :  $("#pjgl_gsId").combotree('getValue');
        var gsMc = $("#pjgl_gsId").combotree('getText') == null ? null :  encodeURI(encodeURI($("#pjgl_gsId").combotree('getText')));
        var khhMc = encodeURI(encodeURI($("#pjgl_khhId").val()));
        var pjStartDate = $("[name='pjStartDate']").val();
        var pjEndDate = $("[name='pjEndDate']").val();
        var pjhm = $("#pjgl_pjhm").val();
        var zt = $('#pj_zt').combobox('getValue') == null ? null : $('#pj_zt').combobox('getValue');
        window.open($.webPath() + "/zjgl/pjgl/pjgl_export?pjsflxType="+pjsflxType+"&&pjlxType="
            +pjlxType+"&&gsId="+
            gsId+"&&gsMc="+gsMc+"&&khhMc="+khhMc
            +"&&pjStartDate="+pjStartDate+"&&pjEndDate="+pjEndDate+"&&pjhm="+pjhm+"&&zt="+zt);
    });

//    //双击事件
//    $("#grid_pjgl").datagrid({
//        onDblClickCell:function (index, field) {
//            $('#grid_pjgl').datagrid('selectRow',index);
//                if (field == "nbgs_jc"){
//                    $("#btn_pjgl_mod :visible").click();
//            }
//        }
//    });
});

//新增票据
function pjgl_add(title, action) {
    var href = $.webPath() + "/zjgl/pjgl/pjgl_content";
    var actionUrl = $.webPath() + "/zjgl/pjgl/add_pjgl";
    pjgl_showDialog(title, href, actionUrl);
}

//票据贴现
function pjgl_pjtx(title, action) {
    var rows = $("#grid_pjgl").datagrid("getSelections");
    var id = rows[0].nbgs_jc;
    var bankid = rows[0].yscpyh
    var  pjIdList = "";
    for(var i=0;i<rows.length;i++){
//        if(id != rows[i].nbgs_jc){
//            $.messager.alert("提示", "请选择同一家公司票据贴现!", "warning");
//            return;
//        }
//        if(bankid != rows[i].yscpyh){
//            $.messager.alert("提示", "请选择同一家银行票据贴现!", "warning");
//            return;
//        }
        if(rows[i].status != 1){
            $.messager.alert("提示", "请选择可操作的票据!", "warning");
            return;
        }
        pjIdList += rows[i].id+",";
    }
    pjIdList = pjIdList.substring(0,pjIdList.length-1);
    var href = $.webPath() + "/zjgl/pjgl/pjgl_pjtx?pjIds="+pjIdList;
    var actionUrl = $.webPath() + "/zjgl/pjgl/"+action;
    pjgl_showTx(title, href, actionUrl);
}

//票据托收
function pjgl_pjts(title, action) {
    var rows = $("#grid_pjgl").datagrid("getSelections");
    var id = rows[0].nbgs_jc;
    var bankid = rows[0].yscpyh
    var  pjIdList = "";
    for(var i=0;i<rows.length;i++){
//        if(id != rows[i].nbgs_jc){
//            $.messager.alert("提示", "请选择同一家公司票据托收!", "warning");
//            return;
//        }
        if(rows[i].status != 1){
            $.messager.alert("提示", "请选择可操作的票据!", "warning");
            return;
        }
//        if(bankid != rows[i].yscpyh){
//            $.messager.alert("提示", "请选择同一家银行票据托收!", "warning");
//            return;
//        }
        pjIdList += rows[i].id+",";
    }
    pjIdList = pjIdList.substring(0,pjIdList.length-1);
    var href = $.webPath() + "/zjgl/pjgl/pjgl_pjts?pjIds="+pjIdList;
    var actionUrl = $.webPath() + "/zjgl/pjgl/"+action;
    pjgl_showTs(title, href, actionUrl);
}

//票据承兑
function pjgl_pjcd(title, action) {
    var rows = $("#grid_pjgl").datagrid("getSelections");
    var id = rows[0].nbgs_jc;
    var bankid = rows[0].yfcpyh_qc
    var  pjIdList = "";
    for(var i=0;i<rows.length;i++){
        if(id != rows[i].nbgs_jc){
            $.messager.alert("提示", "请选择同一家公司票据承兑!", "warning");
            return;
        }
        if(bankid != rows[i].yfcpyh_qc){
            $.messager.alert("提示", "请选择同一家银行票据承兑!", "warning");
            return;
        }
        if(rows[i].status != 5){
            $.messager.alert("提示", "请选择已使用的票据承兑!", "warning");
            return;
        }
        pjIdList += rows[i].id+",";
    }
    pjIdList = pjIdList.substring(0,pjIdList.length-1);
    var href = $.webPath() + "/zjgl/pjgl/pjgl_pjcd?pjIds="+pjIdList;
    var actionUrl = $.webPath() + "/zjgl/pjgl/"+action;
    pjgl_showCd(title, href, actionUrl);
}

//修改票据
function pjgl_modefy(title, action) {
    var rows = $("#grid_pjgl").datagrid("getSelections");
    if(rows.length>0){
        if(rows[0].status != 1){
            $.messager.alert("提示", "请选择可操作的票据!", "warning");
            return;
        }
        if (rows.length>1) {
            $.messager.alert("提示", "一次只能更新一个记录!", "warning");
            return;
        }
        var href = $.webPath() + "/zjgl/pjgl/pjgl_content?pjId="+rows[0].id;
        var actionUrl = $.webPath() + "/zjgl/pjgl/update_pjgl";
        pjgl_showDialog(title, href, actionUrl);
    }else{
        $.messager.alert("提示", "请选择要更新的记录!", "warning");
        return;
    }
}

//票据兑换
function pjgl_dh(title, action) {
    var rows = $("#grid_pjgl").datagrid("getSelections");
    var type = $("[name='pjsflx_type']").val();
//    var type = $("[name='pjsflx_type']").combobox("getValue");
    if (rows.length>0){
        if(rows[0].status != 1){
            $.messager.alert("提示", "请选择可操作的票据!", "warning");
            return;
        }
        if(type != 0){
            $.messager.alert("提示", "请选择应收票据!", "warning");
            return;
        }
        if (rows.length>1) {
            $.messager.alert("提示", "一次只能兑换一张票据!", "warning");
            return;
        }
        var href = $.webPath() + "/zjgl/pjgl/pjgl_pjdh?pjId="+rows[0].id;
        var actionUrl = $.webPath() + "/zjgl/pjgl/pjdh_pjgl?pjId="+rows[0].id;
        pjgl_dhDialog(title, href, actionUrl);
    }else{
        $.messager.alert("提示", "请选择要兑换的记录!", "warning");
        return;
    }
}


//弹出兑换的新增或修改的窗口
function pjgl_dhDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:760,
        height:430,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'确认',
                handler:function () {
                    $(win).mask();
                    $('#form_pjgl_pjdh').form('submit', {
                        url:actionUrl,
                        onSubmit:function (param) {
                            var r = $('#pjdh_pj_grid').edatagrid("getSelected");
                            var index = $('#pjdh_pj_grid').edatagrid("getRowIndex", r);
                            $('#pjdh_pj_grid').edatagrid("endEdit", index);
                            var rows = $("#pjdh_pj_grid").datagrid("getRows");
                            var je = 0;
                            if (rows) {
                                for (var i = 0; i < rows.length; i++) {
                                    je = je + parseFloat(rows[i].dqje);
                                }
                                if ( je > $("#pjdh_pjje").val()) {
                                    $(win).mask('hide');
                                    $.messager.alert('提示', '兑换金额不得大于票据金额', 'error');
                                    return false;
                                }
                                if ( je < $("#pjdh_pjje").val()) {
                                    $.messager.alert('提示', '兑换金额小于票据金额,请操作员注意', 'error');
                                }
                                    var isValid = $(this).form('validate');
                                    if (!isValid) {
                                        $(win).mask('hide');
                                        return false;
                                    }
                                    var data = $('#pjdh_pj_grid').edatagrid("getData");
                                    if (!data.rows.length) {
                                        $(win).mask('hide');
                                        $.messager.alert("提示", "请填写!", "waring");
                                        return false;
                                    }
                                    var arrayPrefix = 'pjdh.pjdhList';
                                    //票据兑换明细
                                    var paramString = "{";
                                    //票据兑换明细
                                    for (var i = 0; i < data.rows.length; i++) {
                                        var objectString = $.toJSONStringPrefix(data.rows[i], arrayPrefix + "[" + i + "].");
                                        paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                                    }
                                    if (data.rows && data.rows.length > 0)
                                        paramString = paramString.substring(0, paramString.length - 1);
                                    paramString += "}";
                                    $.extend(param, $.parseJSON(paramString));
                                    return isValid;
                            }
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            if (data == "票据兑换成功") {
                                $(win).window('close');
                                $('#grid_pjgl').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:data,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', data, 'error');
                        }
                    });
                }
            }
        ]
    });
}


//弹出新增或修改的窗口
function pjgl_showDialog(title, href, actionUrl) {
    var win = $.window({
        onBeforeClose:function(){
            $(".validatebox-tip").remove();
        },
        title:title,
        width:750,
        height:430,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'确认',
                handler:function () {
                    $(win).mask();
                    $('#form_pjgl').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_pjgl').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}


//弹出贴现窗口
function pjgl_showTx(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:750,
        height:430,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'确认',
                handler:function () {
                    $(win).mask();
                    $('#form_pjgl_pjtx').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_pjgl').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

function pjgl_txqx() {
    var rows = $("#grid_pjgl").datagrid("getSelections");
    if(rows.length>1){
        $.messager.alert("提示", "只可以选择一条票据取消贴现！", "warning");
    }
    if (rows.length==1) {
        $.messager.confirm('提示', '是否取消该票据贴现？', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/zjgl/pjgl/pjgl_qxtx",
                    dataType: 'text',
                    type: 'POST',
                    data: {pjId: rows[0].id},
                    beforeSend: function () {
                        $("#pjgl_layout").mask();
                    },
                    success: function (data) {
                        $("#pjgl_layout").mask("hide");
                        if (data=='票据已取消贴现!') {
                            $('#grid_pjgl').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data, "error");
                        }
                    }
                });
            }
        });
    } else if(rows.length==0)  {
        $.messager.alert("提示", "请选择要取消的贴现票据！", "warning");
    }
}

function pjgl_tsqx() {
    var rows = $("#grid_pjgl").datagrid("getSelections");
    if(rows.length>1){
        $.messager.alert("提示", "只可以选择一条票据取消托收！", "warning");
    }
    if (rows.length==1) {
        $.messager.confirm('提示', '是否取消该票据托收？', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/zjgl/pjgl/pjgl_qxts",
                    dataType: 'text',
                    type: 'POST',
                    data: {pjId: rows[0].id},
                    beforeSend: function () {
                        $("#pjgl_layout").mask();
                    },
                    success: function (data) {
                        $("#pjgl_layout").mask("hide");
                        if (data=='票据已取消托收!') {
                            $('#grid_pjgl').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data, "error");
                        }
                    }
                });
            }
        });
    } else if(rows.length==0)  {
        $.messager.alert("提示", "请选择要取消的托收票据！", "warning");
    }
}

function pjgl_cdqx() {
    var rows = $("#grid_pjgl").datagrid("getSelections");
    if(rows.length>1){
        $.messager.alert("提示", "只可以选择一条票据取消承兑！", "warning");
    }
    if (rows.length==1) {
        $.messager.confirm('提示', '是否取消该票据承兑？', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/zjgl/pjgl/pjgl_qxcd",
                    dataType: 'text',
                    type: 'POST',
                    data: {pjId: rows[0].id},
                    beforeSend: function () {
                        $("#pjgl_layout").mask();
                    },
                    success: function (data) {
                        $("#pjgl_layout").mask("hide");
                        if (data=='票据已取消承兑!') {
                            $('#grid_pjgl').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data, "error");
                        }
                    }
                });
            }
        });
    } else if(rows.length==0)  {
        $.messager.alert("提示", "请选择要取消的承兑票据！", "warning");
    }
}

function pjgl_dhqx() {
    var rows = $("#grid_pjgl").datagrid("getSelections");
    if(rows.length>1){
        $.messager.alert("提示", "只可以选择一条票据取消兑换！", "warning");
    }
    if (rows.length==1) {
        $.messager.confirm('提示', '是否取消该票据兑换？', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/zjgl/pjgl/pjgl_qxdh",
                    dataType: 'text',
                    type: 'POST',
                    data: {pjId: rows[0].id},
                    beforeSend: function () {
                        $("#pjgl_layout").mask();
                    },
                    success: function (data) {
                        $("#pjgl_layout").mask("hide");
                        if (data=='票据已取消兑换!') {
                            $('#grid_pjgl').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data, "error");
                        }
                    }
                });
            }
        });
    } else if(rows.length==0)  {
        $.messager.alert("提示", "请选择要取消的兑换票据！", "warning");
    }
}

//弹出承兑窗口
function pjgl_showCd(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:750,
        height:430,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'确认',
                handler:function () {
                    $(win).mask();
                    $('#form_pjgl_pjcd').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_pjgl').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//弹出托收窗口
function pjgl_showTs(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:750,
        height:430,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'确认',
                handler:function () {
                    $(win).mask();
                    $('#form_pjgl_pjts').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_pjgl').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}



//删除票据
function pjgl_delete() {
    var row = $('#grid_pjgl').datagrid('getSelected');
    if(row.status !=1){
        $.messager.alert("提示", "请删除可操作的票据！", "warning");
        return ;
    }
    if (row) {
        $.messager.confirm('提示', '是否删除该票据？', function (r) {
            if (r) {
                var index = $('#grid_pjgl').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/zjgl/pjgl/delete_pjgl?pjId="+row.id,
                    dataType: 'json',
                    type: 'POST',
                    data: {pjglId:row.id},
                    beforeSend: function () {
                        $("#layout_pjgl").mask();
                    },
                    success:function(data){
                        $("#layout_pjgl").mask("hide");
                        if(data.statusCode == 200){
                            $('#grid_pjgl').datagrid('deleteRow', index);
                            $('#grid_pjgl').datagrid('reload');
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        }else{
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要删除的票据！", "warning");
    }
}

//查询票据
function pjgl_search() {
    var gsMc =  $("#pjgl_gsId").combotree('getText') == null ? null :  $("#pjgl_gsId").combotree('getText');
    var gsId =  $("#pjgl_gsId").combotree('getValue') == null ? null :  $("#pjgl_gsId").combotree('getValue');
    var zt = $('#pj_zt').combobox('getValue') == null ? null : $('#pj_zt').combobox('getValue');
    $('#grid_pjgl').datagrid('load',
        {
//            pjsflxType: $("[name='pjsflx_type']").val(),
            pjlxType: $("[name='pjlx_type']").val(),
            khztType: $("[name='khzt_type']").val(),
            gsId: gsId ,
            gsMc: gsMc,
            khhMc: $("#pjgl_khhId").val(),
            pjStartDate:$("[name='pjStartDate']").val(),
            pjEndDate: $("[name='pjEndDate']").val(),
            pjhm:$("#pjgl_pjhm").val(),
            zt:zt
        }
    );
}
